// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#ifndef OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_FIRMWARE_FI_RNG_FI_H_
#define OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_FIRMWARE_FI_RNG_FI_H_

#include "sw/device/lib/base/status.h"
#include "sw/device/lib/ujson/ujson.h"

/**
 * rng_fi_entropy_src_bias command handler.
 *
 * Configures the entropy source module to return data on the ENTROPY_DATA
 * register. The goal is to inject a fault such that a BIAS is visible.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_entropy_src_bias(ujson_t *uj);

/**
 * rng_fi_firmware_override command handler.
 *
 * Configures the entropy source module with the firmware override mode allowing
 * the test to directly access the generated entropy bits.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_firmware_override(ujson_t *uj);

/**
 * rng_fi_edn_bias command handler.
 *
 * Induce a bias into the random data generated by the EDN.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_edn_bias(ujson_t *uj);

/**
 * rng_fi_edn_resp_ack command handler.
 *
 * Generate random data. Try to inject fault during bus ack to receive the same
 * random data multiple times.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_edn_resp_ack(ujson_t *uj);

/**
 * Initializes the trigger and configures the device for the EDN FI test.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_edn_init(ujson_t *uj);

/**
 * csrng_bias command handler.
 *
 * Generate known data using the CSRNG.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_csrng_bias(ujson_t *uj);

/**
 * rng_fi_entropy_src_bias_fw_override command handler.
 *
 * Write a fixed seed into the FW_OV_WR_DATA register in the FW override mode.
 * This seed goes through the conditioner into the CSRNG. The software interface
 * of the CSRNG is used to extract the random data.
 *
 * Faults are injected during the trigger_high & trigger_low.
 *
 * @param uj An initialized uJSON context.
 * @param static_seed Use static seed or seed provided by user.
 * @return OK or error.
 */
status_t handle_rng_fi_csrng_bias_fw_override(ujson_t *uj, bool static_seed);

/**
 * Initializes the trigger and configures the device for the CSRNG FI test.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi_csrng_init(ujson_t *uj);

/**
 * Rng FI command handler.
 *
 * Command handler for the Rng FI command.
 *
 * @param uj An initialized uJSON context.
 * @return OK or error.
 */
status_t handle_rng_fi(ujson_t *uj);

#endif  // OPENTITAN_SW_DEVICE_TESTS_PENETRATIONTESTS_FIRMWARE_FI_RNG_FI_H_
